//
//  ZZCQuotaController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/22.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCQuotaController.h"
#import "ZZCQuotaCell.h"
#import "ZZCCreditController.h"
#import "ZZCOwnPropertyController.h"
#import "ZZCRefundController.h"
#import "ZZCQuota.h"

static NSString *quotaCell = @"quotaCell";
@interface ZZCQuotaController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *quotaTableView;
@property (strong, nonatomic) NSMutableArray *quotaArray;
@property (strong, nonatomic) NSArray *quotaData;
@property (strong, nonatomic) NSMutableArray *createIdArr;
@end

@implementation ZZCQuotaController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHex:0xeaeaea];
    [self.view addSubview:self.quotaTableView];
    if ([self.sift isEqualToString:@"sift"]) {
        self.title = @"额度";
        self.quotaTableView.frame = CGRectMake(0, 1, ZZCScreenW, ZZCScreenH-65);
    }else {
      [self bottomBtn];
    }
    [self requestQuotaData];
    //监听授信成功的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(creditSucceedData) name:@"creditSucceed" object:nil];
}
//授信成功重新获取列表
- (void)creditSucceedData {
    [self requestQuotaData];
}

- (void)requestQuotaData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_CREDIT_MYQUOTA parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            self.quotaData = [ZZCQuota mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            [self.quotaTableView reloadData];
            NSMutableArray *arr = responseObject[@"data"][@"list"];
            NSLog(@"%@",arr);
//            for (NSArray * in <#collection#>) {
//                <#statements#>
//            }
        }
    }];
}



- (void)bottomBtn {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, ZZCScreenH-157, ZZCScreenW, 49)];
    btn.backgroundColor = [UIColor colorWithHex:0xEA6E25];
    [btn setTitle:@"申请授信" forState:0];
    [btn addTarget:self action:@selector(extendCreditClick) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = FONT(19);
    [self.view addSubview:btn];
}

//再次授信按钮监听
- (void)extendCreditClick {
    ZZCLog(@"你点击了申请授信按钮");
    ZZCCreditController *credit = [ZZCCreditController new];
    [self.parentController.navigationController pushViewController:credit animated:YES];
}
#pragma mark  - 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //填你后台返个数
    return self.quotaData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZZCQuotaCell *cell = [tableView dequeueReusableCellWithIdentifier:quotaCell];
    cell.selectionStyle = 0;
    //给模型传值过去
    cell.quotaModel = self.quotaData[indexPath.row];
    cell.refundBtn.tag = [cell.quotaModel.id integerValue];
    [cell.refundBtn addTarget:self action:@selector(refundBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)refundBtnClick:(UIButton *)sender {
    ZZCLog(@"你点了还款--%ld",(long)sender.tag);
    ZZCRefundController *refund = [ZZCRefundController new];
    refund.createID = sender.tag;
    [self.parentController.navigationController pushViewController:refund animated:YES];
}

- (UITableView *)quotaTableView {
    if (_quotaTableView == nil) {
        _quotaTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 1, ZZCScreenW, ZZCScreenH-159)];
        [_quotaTableView registerNib:[UINib nibWithNibName:@"ZZCQuotaCell" bundle:nil] forCellReuseIdentifier:quotaCell];
        _quotaTableView.backgroundColor = [UIColor colorWithHex:0xeaeaea];
        _quotaTableView.delegate = self;
        _quotaTableView.dataSource = self;
        _quotaTableView.separatorStyle = NO;
        _quotaTableView.showsVerticalScrollIndicator = NO;
        _quotaTableView.rowHeight = 200;
    }
    return _quotaTableView;
}

- (NSMutableArray *)quotaArray {
    if (_quotaArray == nil) {
        _quotaArray = [NSMutableArray array];
    }
    return _quotaArray;
}

- (NSArray *)quotaData {
    if (_quotaData == nil) {
        _quotaData = [NSArray array];
    }
    return _quotaData;
}
@end
